package cn.stylefeng.guns.modular.userinfo.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.userinfo.entity.UserInfo;
import cn.stylefeng.guns.modular.userinfo.mapper.UserInfoMapper;
import cn.stylefeng.guns.modular.userinfo.model.params.UserInfoParam;
import cn.stylefeng.guns.modular.userinfo.model.result.UserInfoResult;
import  cn.stylefeng.guns.modular.userinfo.service.UserInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 扩展用户信息表 服务实现类
 * </p>
 *
 * @author dong
 * @since 2023-04-22
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Override
    public void add(UserInfoParam param){
        UserInfo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(UserInfoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(UserInfoParam param){
        UserInfo oldEntity = getOldEntity(param);
        UserInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public UserInfoResult findBySpec(UserInfoParam param){
        return null;
    }

    @Override
    public List<UserInfoResult> findListBySpec(UserInfoParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(UserInfoParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(UserInfoParam param){
        return param.getUserId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private UserInfo getOldEntity(UserInfoParam param) {
        return this.getById(getKey(param));
    }

    private UserInfo getEntity(UserInfoParam param) {
        UserInfo entity = new UserInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
